## read in 1km fire scar ascii 2000 - 2009, turn them into a data frme and get unqiue values after rounding 5km

#list the libraries needed
necessary=c("SDMTools","adehabitat",'maptools', 'foreign')
#check if library is installed
installed = necessary %in% installed.packages()
#if library is not installed, install it
if (length(necessary[!installed]) >=1) install.packages(necessary[!installed], dep = T)

#load the libraries
for (lib in necessary) library(lib,character.only=T)
library (adehabitat)

# set the working and output directories

workdir = "C:/pyrogeography/FIRESCARASCII/"
outdir = "C:/pyrogeography/FIRESCARASCII/outdir/"
pos.file = "C:/pyrogeography/FIRESCARASCII/jpOzmask.csv"
# read in the ascii

files = list.files (workdir, pattern = "rastert")
print(files)

files = "rastert_fire00.asc"

for (ff in files) {
      tasc = read.asc (paste(workdir, ff, sep = ""))
      }

pos = read.csv (pos.file, as.is = T)
tname = gsub ("\\\\.asc", "", 2000); tname = paste ("Ym", tname, sep = "")
pos [tname] = extract.data (cbind(pos$lon, pos$lat), tasc)
pos1 = na.omit (pos)
pos2 = subset(pos1, Ym2000 > 0, select = c(lat, lon, Ym2000))
pos2$year = "2000"
fire2000 = pos2
fire2000$species = "fire"

# set up each year as :

namesind=function(df){

temp1=names(df)
temp2=seq(1,length(temp1))
temp3=data.frame(temp1,temp2)
names(temp3)=c("VAR","COL")
return(temp3)
rm(temp1,temp2,temp3)
}
ni <- namesind
ni (fire2000)

fire2000=fire2000[c(5,1,2,3,4)]
names(fire2000)=c("species","LONGDEC","LATDEC","month","year")
fire2000[1:10,]


# bind all the years together in a mega table

fire0009 = rbind (FS2000, FS2001, FS2002, FS2003, FS2004, FS2005, FS2006, FS2007, FS2008, FS2009)
fire0009 [1:10,]
dim (fire0009)
write.csv (fire0009, "fire0009_5km_unique.csv")


